package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.ACCOUNTBean;
import com.kpx.app.bean.CertificationInfoBean;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.bean.ThirdInfoBean;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.view.ViewCertification;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/11.
 */

public class ActivityBindThirdTo extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.txt_left)
    TextView txtLeft;
    @BindView(R.id.et_content)
    EditText etContent;
    public static ThirdInfoBean thirdNum;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_bind_now);
        ButterKnife.bind(this);
        if(thirdNum==null){
            finish();
            return;
        }
        UIUtils.setNormalTitle(titleview, "绑定" + thirdNum.getPlatform() + "账号");
        titleview.setRight("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSave();
            }
        }, R.color.white);
        txtLeft.setText(thirdNum.getPlatform() + "会员账号:");
        etContent.setHint("请输入" + thirdNum.getPlatform() + "账号");
    }

    private void toSave() {
        if (TextUtils.isEmpty(etContent.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityBindThirdTo.this, "请输入会员名称");
            return;
        }
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        MyHashMap map1 = new MyHashMap();
        map1.put(thirdNum.getDmId() + "", etContent.getText().toString().trim());
        map.put("thirdNum", map1.toString());
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    ToastUtil.getInstance().showToast(ActivityBindThirdTo.this, "绑定成功");
                    getRefreshData();
                } else {
                    ToastUtil.getInstance().showToast(ActivityBindThirdTo.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityBindThirdTo.this, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_BIND_THIRD_INFO, true, map);
    }

    private void getRefreshData() {
        MyHashMap map = new MyHashMap();
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    CertificationInfoBean b = (CertificationInfoBean) result.getData();
                    if (b != null) {
                        LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
                        loginUser.setACCOUNT((ArrayList<ACCOUNTBean>) b.getUserThirdAccount());
                        UserInfoUtils.saveLoginUser(loginUser);
                        finish();
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityBindThirdTo.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityBindThirdTo.this, error);
            }

        }, CertificationInfoBean.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_CHECK_CERTIFICATION, true, map);

    }

    @Override
    public void initData() {

    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

}
